import defaultSettings from '@/settings'
import { setTheme, getTheme } from '@/utils/auth'
// 页面的头部固定 侧边栏的logo
const { showSettings, fixedHeader, sidebarLogo, theme } = defaultSettings

const state = {
  showSettings: showSettings,
  fixedHeader: fixedHeader,
  sidebarLogo: sidebarLogo,
  // 默认颜色 优先从本地获取
  theme: getTheme() || theme
}
const mutations = {
  CHANGE_SETTING: (state, { key, value }) => {
    // 检测对象是否有某个属性
    console.log(key, value)
    // eslint-disable-next-line no-prototype-builtins
    if (state.hasOwnProperty(key)) {
      state[key] = value
    }
    // 如果是修改了颜色 将最新的颜色存储到本地
    if (key === 'theme') {
      setTheme(value)
    }
  },
  // 重置主题色的mutation
  resetTheme(state) {
    state.theme = theme
  }
  /* setTheme(state, newTheme) {
    state.theme = newTheme
  } */
}

const actions = {
  changeSetting({ commit }, data) {
    console.log(data)
    commit('CHANGE_SETTING', data) //  {key: 'theme', value: '#ccc'}
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

